/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weissenborn.colabtex.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class ResourceViewComposite extends Composite {

	/** The Constant UPLOAD_ACTION_URL. */
	private static final String UPLOAD_ACTION_URL = GWT.getModuleBaseURL() + "upload";
	private ListBox resourceBox;
	
	/**
	 * Instantiates a new resource view composite.
	 *
	 * @param parent the parent
	 */
	public ResourceViewComposite(final TexView.Presenter parent) {
		
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setSpacing(20);
		verticalPanel.setStyleName("colabtex-Panel");
		initWidget(verticalPanel);		
		verticalPanel.setSize("100%", "415px");
		
		Label lblViewAndModify = new Label("View and modify the resources within this group!");
		lblViewAndModify.setStyleName("gwt-Label-Login");
		verticalPanel.add(lblViewAndModify);
		verticalPanel.setCellHeight(lblViewAndModify, "15");
		
		resourceBox = new ListBox();
		resourceBox.setVisibleItemCount(5);
		verticalPanel.add(resourceBox);
		verticalPanel.setCellHeight(resourceBox, "150");
		resourceBox.setSize("300px", "150px");
		
		final FormPanel formPanel = new FormPanel();		
		
		verticalPanel.add(formPanel);
		verticalPanel.setCellVerticalAlignment(formPanel, HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel.setCellHeight(formPanel, "30px");
		formPanel.setSize("300px", "30px");
		
		final FileUpload fileUpload = new FileUpload();
		formPanel.setWidget(fileUpload);
		fileUpload.setSize("100%", "100%");
		
		fileUpload.setName("uploadFormElement");
		
		formPanel.setAction(UPLOAD_ACTION_URL);
		formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {
			
			/* (non-Javadoc)
			 * @see com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler#onSubmitComplete(com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent)
			 */
			public void onSubmitComplete(SubmitCompleteEvent event) {
				resourceBox.addItem(fileUpload.getFilename().substring(fileUpload.getFilename().lastIndexOf("/")+1));
			}
		});
		  
		// Because we're going to add a FileUpload widget, we'll need to set the
		// form to use the POST method, and multipart MIME encoding.
		formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
		formPanel.setMethod(FormPanel.METHOD_POST);
		
		formPanel.addSubmitHandler(new SubmitHandler() {
			public void onSubmit(SubmitEvent event) {
				if (fileUpload.getFilename().length() == 0) {
		            Window.alert("Must choose a file to upload!");
		            event.cancel();
		        }
			}
		});
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		
		Button btnUpload = new Button("upload resource");
		btnUpload.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				formPanel.submit();
			}
		});
		horizontalPanel.add(btnUpload);
		
		Button btnDeleteResource = new Button("delete resource");
		btnDeleteResource.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(resourceBox.getSelectedIndex()>-1)
					parent.deleteResource(resourceBox.getItemText(resourceBox.getSelectedIndex()));
			}
		});
		horizontalPanel.add(btnDeleteResource);		
	}
	
	/**
	 * Gets the resource box.
	 *
	 * @return the resource box
	 */
	public ListBox getResourceBox() {
		return resourceBox;
	}

}
